﻿namespace CatchBall
{
    using System;

    public class Ball
    {
        private Random randomGenerator = new Random();

        // initializes Ball
        // has to be named like this by default - sorry for the nessesary rename
        public Ball()
        {
            this.Row = this.randomGenerator.Next(60); 
            if (this.Row % 2 == 0)
            {
                this.Row++;
            }

            this.Col = this.randomGenerator.Next(20);
            if (this.Col % 2 == 0)
            {
                this.Col++;
            }
        }

        // Now I can access the row and column of the ball by ball.Row and ball.Col, but only the class can change them
        public int Row { get; set; }

        public int Col { get; set; }

        // moves the ball
        public void MoveBall()
        {
            int movement = this.randomGenerator.Next(8);

            // remember the old position
            int rowToClear = this.Row;
            int colToClear = this.Col;

            // change the Ball's current position
            switch (movement)
            {
                case 0: // left
                    this.Col -= 2;
                    break;
                case 1: // up-left
                    this.Col -= 2;
                    this.Row -= 2;
                    break;
                case 2: // up
                    this.Row -= 2;
                    break;
                case 3: // up-right
                    this.Row -= 2;
                    this.Col += 2;
                    break;
                case 4: // right
                    this.Col += 2;
                    break;
                case 5: // down-right
                    this.Col += 2;
                    this.Row += 2;
                    break;
                case 6: // down
                    this.Row += 2;
                    break;
                case 7: // down-left
                    this.Row += 2;
                    this.Col -= 2;
                    break;
            }

            this.KeepInBoundaries();
            this.ClearPreviousBall(rowToClear, colToClear);
            this.VisualizeBall();
        }

        // draws a ball on its current position
        public void VisualizeBall()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(this.Row, this.Col);
            Console.Write('■');
            Console.ResetColor();
        }

        // Deletes the ball from its previous position
        private void ClearPreviousBall(int row, int col)
        {
            Console.SetCursorPosition(row, col);
            Console.Write('■');
        }

        // returns the ball in the field if it tries to escape
        private void KeepInBoundaries()
        {
            if (this.Row < 1)
            {
                this.Row += 2;
            }

            if (this.Row > 59)
            {
                this.Row -= 2;
            }

            if (this.Col < 1)
            {
                this.Col += 2;
            }

            if (this.Col > 19)
            {
                this.Col -= 2;
            }
        }
    }
}
